{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Complex Number Multiplication"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #string #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #字符串 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: complexNumberMultiply"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #复数乘法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p><a href=\"https://baike.baidu.com/item/%E5%A4%8D%E6%95%B0/254365?fr=aladdin\" target=\"_blank\">复数</a> 可以用字符串表示，遵循 <code>\"<strong>实部</strong>+<strong>虚部</strong>i\"</code> 的形式，并满足下述条件：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>实部</code> 是一个整数，取值范围是 <code>[-100, 100]</code></li>\n",
    "\t<li><code>虚部</code> 也是一个整数，取值范围是 <code>[-100, 100]</code></li>\n",
    "\t<li><code>i<sup>2</sup> == -1</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>给你两个字符串表示的复数 <code>num1</code> 和 <code>num2</code> ，请你遵循复数表示形式，返回表示它们乘积的字符串。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num1 = \"1+1i\", num2 = \"1+1i\"\n",
    "<strong>输出：</strong>\"0+2i\"\n",
    "<strong>解释：</strong>(1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i ，你需要将它转换为 0+2i 的形式。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num1 = \"1+-1i\", num2 = \"1+-1i\"\n",
    "<strong>输出：</strong>\"0+-2i\"\n",
    "<strong>解释：</strong>(1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i ，你需要将它转换为 0+-2i 的形式。 \n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>num1</code> 和 <code>num2</code> 都是有效的复数表示。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [complex-number-multiplication](https://leetcode.cn/problems/complex-number-multiplication/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [complex-number-multiplication](https://leetcode.cn/problems/complex-number-multiplication/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"1+1i\"\\n\"1+1i\"', '\"1+-1i\"\\n\"1+-1i\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        plus1=num1.find('+')\n",
    "        plus2=num2.find('+')\n",
    "        re1=int(num1[:plus1])\n",
    "        im1=int(num1[plus1+1:len(num1)-1])\n",
    "        re2=int(num2[:plus2])\n",
    "        im2=int(num2[plus2+1:len(num2)-1])\n",
    "        re=re1*re2-im1*im2\n",
    "        im=re1*im2+re2*im1\n",
    "        return str(re)+'+'+str(im)+'i'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def s2int(text):\n",
    "        position = text.find(\"+\")\n",
    "        a=int(text[:position])\n",
    "        position2 = text.find(\"i\")\n",
    "        if text[position+1]==\"-\":\n",
    "            b=-int(text[position+2:position2])\n",
    "        else:\n",
    "            b=int(text[position+1:position2])\n",
    "        return a,b\n",
    "\n",
    "class Solution:\n",
    "    \n",
    "\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        a1,b1=s2int(num1)\n",
    "        a2,b2=s2int(num2)\n",
    "        x=a1*a2-b1*b2\n",
    "        y=a1*b2+a2*b1\n",
    "\n",
    "        return str(x)+\"+\"+str(y)+\"i\"\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        real1, imag1 = map(int, num1[:-1].split('+'))\n",
    "        real2, imag2 = map(int, num2[:-1].split('+'))\n",
    "        return f'{real1*real2-imag1*imag2}+{real1*imag2+imag1*real2}i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "\n",
    "        R1 = int(num1.split('+')[0])\n",
    "        R2 = int(num2.split('+')[0])\n",
    "\n",
    "        I1 = int(num1.split('+')[1][:-1])\n",
    "        I2 = int(num2.split('+')[1][:-1])\n",
    "\n",
    "        print(R1,R2,I1,I2)\n",
    "\n",
    "        R = R1*R2 - I1*I2\n",
    "        I = R1*I2 + R2*I1\n",
    "\n",
    "        return str(R)+'+'+str(I)+'i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        t = [0,0]\n",
    "        f = [0,0]\n",
    "        n = [str(i) for i in range(10)]\n",
    "        pos = 1\n",
    "        tmpnum = 0\n",
    "        for i in num1:\n",
    "            if i == '-':\n",
    "                pos = -1\n",
    "            elif i in n:\n",
    "                tmpnum = tmpnum*10 + int(i)\n",
    "            elif i == '+':\n",
    "                t[0] = pos * tmpnum\n",
    "                pos = 1\n",
    "                tmpnum = 0\n",
    "            elif i == 'i':\n",
    "                f[0] = pos * tmpnum\n",
    "                pos = 1\n",
    "                tmpnum = 0\n",
    "        for i in num2:\n",
    "            if i == '-':\n",
    "                pos = -1\n",
    "            elif i in n:\n",
    "                tmpnum = tmpnum*10 + int(i)\n",
    "            elif i == '+':\n",
    "                t[1] = pos * tmpnum\n",
    "                pos = 1\n",
    "                tmpnum = 0\n",
    "            elif i == 'i':\n",
    "                f[1] = pos * tmpnum\n",
    "                pos = 1\n",
    "                tmpnum = 0\n",
    "        print(t, f)\n",
    "        return str(t[0]*t[1] + (-1)*f[0]*f[1]) + '+' + str(t[0]*f[1] + t[1]*f[0]) + 'i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        r1,i1=map(int,num1[:-1].split(\"+\"))\n",
    "        r2,i2=map(int,num2[:-1].split(\"+\"))\n",
    "        r3=r1*r2-i1*i2\n",
    "        i3=r1*i2+r2*i1\n",
    "        return str(r3)+'+'+str(i3)+'i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        a1, b1 = map(int, num1[:-1].split('+'))\n",
    "        a2, b2 = map(int, num2[:-1].split('+'))\n",
    "        \n",
    "        res_a, res_b = a1 * a2 - b1 * b2, a1 * b2 + a2 * b1\n",
    "        return f'{res_a}+{res_b}i'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "\n",
    "        li1 = num1.split('+')\n",
    "        li2 = num2.split('+')\n",
    "\n",
    "        num11 = int(li1[0])\n",
    "        num12 = int(li1[1][:-1])\n",
    "        num21 = int(li2[0])\n",
    "        num22 = int(li2[1][:-1])\n",
    "\n",
    "        first = num11*num21\n",
    "        second = num11*num22+num21*num12\n",
    "        third = (-1)*num12*num22\n",
    "\n",
    "        return str(first+third)+'+'+str(second)+'i'\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        num1 = num1.split(\"+\")\n",
    "        num2 = num2.split(\"+\")\n",
    "        num1[1] = int(num1[1][:-1])\n",
    "        num2[1] = int(num2[1][:-1])\n",
    "        num1[0] = int(num1[0])\n",
    "        num2[0] = int(num2[0])\n",
    "        thing = [num1[0]*num2[0]-num1[1]*num2[1],num1[0]*num2[1]+num2[0]*num1[1]]\n",
    "        return str(thing[0])+\"+\"+str(thing[1])+\"i\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        ans = str(int(num1.split('+')[0])*int(num2.split('+')[0]) - int(num1.split('+')[1][:-1])*int(num2.split('+')[1][:-1])) + '+' + str(int(num1.split('+')[0])*int(num2.split('+')[1][:-1]) + int(num1.split('+')[1][:-1])*int(num2.split('+')[0])) + 'i'\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        c1,c2=num1.split(\"+\"),num2.split(\"+\")\n",
    "        void1,void2=c1[1],c2[1]\n",
    "        a1,b1=int(c1[0]),int(void1[:-1])\n",
    "        a2,b2=int(c2[0]),int(void2[:-1])\n",
    "        newreal=str(a1*a2-b1*b2)\n",
    "        newvoid=str(a1*b2+a2*b1)\n",
    "        return (newreal+\"+\"+newvoid+\"i\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        for i in range(len(num1)):\n",
    "            if num1[i]=='+':\n",
    "                a=int(num1[:i])\n",
    "                x=i\n",
    "                \n",
    "                if num1[i+1]=='-':\n",
    "                    x+=1\n",
    "                    b=-1*int(num1[x+1:len(num1)-1])\n",
    "                else:\n",
    "                    b=int(num1[x+1:len(num1)-1])\n",
    "                break\n",
    "        for i in range(len(num2)):\n",
    "            if num2[i]=='+':\n",
    "                c=int(num2[:i])\n",
    "                x=i\n",
    "                \n",
    "                if num2[i+1]=='-':\n",
    "                    x+=1\n",
    "                    d=-1*int(num2[x+1:len(num2)-1])\n",
    "                else:\n",
    "                    d=int(num2[x+1:len(num2)-1])\n",
    "                break\n",
    "        x=a*c-b*d\n",
    "        y=a*d+c*b\n",
    "        if y<0:\n",
    "            return str(x)+'+'+str(y)+'i'\n",
    "        return str(x)+'+'+str(y)+'i'\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        real1,imag1 = map(int,num1[:-1].split('+'))\n",
    "        real2,imag2 = map(int,num2[:-1].split('+'))\n",
    "        return f'{real1*real2-imag1*imag2}+{real1*imag2+imag1*real2}i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        a1,a2=num1[:-1].split(\"+\")\n",
    "        b1,b2=num2[:-1].split(\"+\")\n",
    "        a1=int(a1)\n",
    "        a2=int(a2)\n",
    "        b1=int(b1)\n",
    "        b2=int(b2)\n",
    "        return str(a1*b1-b2*a2)+\"+\"+str(a1*b2+a2*b1)+\"i\"\n",
    "\n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        # 从“+”分开两边\n",
    "        n1 = num1.split(\"+\")\n",
    "        n2 = num2.split(\"+\")\n",
    "        print(n1, n2)\n",
    "\n",
    "        a1 = int(n1[0])\n",
    "        if n1[1][0] != \"-\":\n",
    "            b1 = int(n1[1][:-1])\n",
    "        else:\n",
    "            b1 = int(n1[1][1:-1])*-1\n",
    "\n",
    "        a2 = int(n2[0])\n",
    "        if n2[1][0] != \"-\":\n",
    "            b2 = int(n2[1][:-1])\n",
    "        else:\n",
    "            b2 = int(n2[1][1:-1])*-1\n",
    "        print(a1, b1, a2, b2)\n",
    "\n",
    "        a = a1*a2   \n",
    "        b = a1*b2 + a2*b1\n",
    "        c = (b1*b2)*-1\n",
    "        new_num = str(a+c) + \"+\" + str(b) + \"i\"\n",
    "        return new_num\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        temp=num1.split('+')\n",
    "        num1_s=int(temp[0])\n",
    "        num1_x=int(temp[1][:-1])\n",
    "        temp=num2.split('+')\n",
    "        num2_s=int(temp[0])\n",
    "        num2_x=int(temp[1][:-1])\n",
    "        result_s=num1_s*num2_s-num1_x*num2_x\n",
    "        result_x=num1_s*num2_x+num1_x*num2_s\n",
    "        return str(result_s)+\"+\"+str(result_x)+\"i\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        temp=num1.split('+')\n",
    "        num1_s=int(temp[0])\n",
    "        num1_x=int(temp[1][:-1])\n",
    "        temp=num2.split('+')\n",
    "        num2_s=int(temp[0])\n",
    "        num2_x=int(temp[1][:-1])\n",
    "        return str(num1_s*num2_s-num1_x*num2_x)+\"+\"+str(num1_s*num2_x+num1_x*num2_s)+\"i\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        a1,b1=map(int,num1[:-1].split('+'))\n",
    "        a2,b2=map(int,num2[:-1].split('+'))\n",
    "        return f\"{a1*a2-b1*b2}+{a1*b2+a2*b1}i\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        real_1, img_1 = num1.rstrip('i').split('+')\n",
    "        real_2, img_2 = num2.rstrip('i').split('+')\n",
    "        return str(int(real_1) * int(real_2) - int(img_1) * int(img_2)) + '+' + str(int(real_1) * int(img_2) + int(real_2) * int(img_1)) + 'i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        all1, all2 = num1.split('+'), num2.split('+')\n",
    "        real1, comp1 = int(all1[0]), int(all1[1][:-1])\n",
    "        real2, comp2 = int(all2[0]), int(all2[1][:-1])\n",
    "        res_real = real1 * real2 - comp1 * comp2\n",
    "        res_comp = real1 * comp2 + real2 * comp1\n",
    "        return f'{res_real}+{res_comp}i'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        num1 = eval(num1.replace(\"i\", \"j\"))\n",
    "        num2 = eval(num2.replace(\"i\", \"j\"))\n",
    "        res = num1 * num2\n",
    "        return \"%d+%di\" % (res.real, res.imag)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        real1, img1 = map(int, num1[:-1].split(\"+\"))\n",
    "        real2, img2 = map(int, num2[:-1].split(\"+\"))\n",
    "        real3 = real1 * real2 - img1 * img2\n",
    "        img3 = real1 * img2 + real2 * img1 \n",
    "        res = str(real3) + \"+\" + str(img3) + \"i\"\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        temp1=num1.split('+')\n",
    "        re1=int(temp1[0]) if temp1[0][0]!='-' else -int(temp1[0][1:])\n",
    "        temp1[1]=temp1[1][:-1]\n",
    "        im1=int(temp1[1]) if temp1[1][0]!='-' else -int(temp1[1][1:])\n",
    "\n",
    "        temp2=num2.split('+')\n",
    "        re2=int(temp2[0]) if temp2[0][0]!='-' else -int(temp2[0][1:])\n",
    "        temp2[1]=temp2[1][:-1]\n",
    "        im2=int(temp2[1]) if temp2[1][0]!='-' else -int(temp2[1][1:])\n",
    "\n",
    "        re=re1*re2-im1*im2\n",
    "        im=re1*im2+re2*im1\n",
    "        return str(re)+'+'+str(im)+'i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        lst=re.findall(r'-?[0-9]{1,3}',num1)\n",
    "        a1,b1=int(lst[0]),int(lst[1])\n",
    "        lst=re.findall(r'-?[0-9]{1,3}',num2)\n",
    "        a2,b2=int(lst[0]),int(lst[1])\n",
    "        b=a1*b2+a2*b1\n",
    "        a=a1*a2-b1*b2\n",
    "        return str(a)+'+'+str(b)+'i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        real1, imag1 = map(int, num1[:-1].split('+'))\n",
    "        real2, imag2 = map(int, num2[:-1].split('+'))\n",
    "        return f'{real1 * real2 - imag1 * imag2}+{real1 * imag2 + imag1 * real2}i'\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        def parse(num: str) -> tuple[int, int]:\n",
    "            real, imaginary = 0, 0\n",
    "            i, n = 0, len(num)\n",
    "\n",
    "            negative = False\n",
    "            if num[i] == \"-\":\n",
    "                negative = True\n",
    "                i += 1\n",
    "            \n",
    "            while num[i] != \"+\":\n",
    "                real = real * 10 + int(num[i])\n",
    "                i += 1\n",
    "            if negative:\n",
    "                real = -real\n",
    "            \n",
    "            negative = False\n",
    "            i += 1\n",
    "            if num[i] == \"-\":\n",
    "                negative = True\n",
    "                i += 1\n",
    "            \n",
    "            while num[i] != \"i\":\n",
    "                imaginary = imaginary * 10 + int(num[i])\n",
    "                i += 1\n",
    "            \n",
    "            if negative:\n",
    "                imaginary = -imaginary\n",
    "            \n",
    "            return real, imaginary\n",
    "        \n",
    "        x1, y1 = parse(num1)\n",
    "        x2, y2 = parse(num2)\n",
    "        x = x1 * x2 - y1 * y2\n",
    "        y = x1 * y2 + y1 * x2\n",
    "        return f\"{x}+{y}i\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        complex1 = num1.split('+')\n",
    "        complex2 = num2.split('+')\n",
    "        a1 = int(complex1[0])\n",
    "        a2 = int(complex2[0])\n",
    "        b1 = int(complex1[1][: -1])\n",
    "        b2 = int(complex2[1][: -1])\n",
    "        real = a1 * a2 - b1 * b2\n",
    "        fake = a1 * b2 + a2 * b1\n",
    "        return str(real) + '+' + str(fake) + 'i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        real1,imag1=map(int,num1[:-1].split('+'))\n",
    "        real2,imag2=map(int,num2[:-1].split('+'))\n",
    "        return f'{real1*real2-imag1*imag2}+{real1*imag2+imag1*real2}i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        k1 = num1.split('+')\n",
    "        k2 = num2.split('+')\n",
    "        real_1, img_1 = int(k1[0]), int(k1[1][:-1])\n",
    "        real_2, img_2 = int(k2[0]), int(k2[1][:-1])\n",
    "        res_1 = real_1 * real_2 - img_1 * img_2\n",
    "        res_2 = real_1 * img_2 + real_2 * img_1\n",
    "        return str(res_1) + '+' + str(res_2) + 'i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        a,b=num1.split('+')\n",
    "        b=b.strip('i')\n",
    "        c,d=num2.split('+')\n",
    "        d=d.strip('i')\n",
    "        a,b,c,d=int(a),int(b),int(c),int(d)\n",
    "        return str(a*c-b*d)+'+'+str(b*c+a*d)+'i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        temp=num1.split('+')\n",
    "        num1_s=int(temp[0])\n",
    "        num1_x=int(temp[1][:-1])\n",
    "        temp=num2.split('+')\n",
    "        num2_s=int(temp[0])\n",
    "        num2_x=int(temp[1][:-1])\n",
    "        result_s=num1_s*num2_s-num1_x*num2_x\n",
    "        result_x=num1_s*num2_x+num1_x*num2_s\n",
    "        return str(result_s)+\"+\"+str(result_x)+\"i\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        # (a0 + b0*i) * (a1 + b1*i) = (a0 * a1 - b0 * b1) + (a0 * b1 + a1 * b0) * i\n",
    "\n",
    "        a0,b0 = map(int,num1[:-1].split(\"+\"))\n",
    "        a1,b1 = map(int,num2[:-1].split(\"+\"))\n",
    "        return \"{}+{}i\".format(a0*a1 - b0*b1, a0*b1 + a1*b0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        def parse_complex( complex_num:str ):\n",
    "            comp_list = [ int(i) for i in complex_num.strip('i').split('+')]\n",
    "            return comp_list\n",
    "        num1 = parse_complex( num1 )\n",
    "        num2 = parse_complex( num2 )\n",
    "        real = num1[0] * num2[0] - num1[1]*num2[1]\n",
    "        ima = num1[0]*num2[1] + num1[1]*num2[0]\n",
    "        return str(real) + '+' + str(ima) + 'i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        a = int(num1.replace('i','').split('+')[0])\n",
    "        b = int(num1.replace('i','').split('+')[1])\n",
    "        c = int(num2.replace('i','').split('+')[0])\n",
    "        d = int(num2.replace('i','').split('+')[1])\n",
    "        x = a*c-b*d\n",
    "        y = a*d+b*c\n",
    "        return str(x)+'+'+str(y)+'i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        num1_real,num1_imag = map(int,num1.replace(\"i\",\"\").split(\"+\"))\n",
    "        num2_real,num2_imag = map(int,num2.replace(\"i\",\"\").split(\"+\"))\n",
    "        return str(num1_real*num2_real-num1_imag*num2_imag)+\"+\"+str(num1_real*num2_imag+num1_imag*num2_real)+\"i\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        real1, imag1 = map(int, num1[:-1].split('+'))\n",
    "        real2, imag2 = map(int, num2[:-1].split('+'))\n",
    "        return f'{real1 * real2 - imag1 * imag2}+{real1 * imag2 + imag1 * real2}i'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        num1 = num1.split('+')\n",
    "        num2 = num2.split('+')\n",
    "\n",
    "        # 拆分为实部和虚部\n",
    "        r1, v1 = int(num1[0]), int(num1[1][0: -1])\n",
    "        r2, v2 = int(num2[0]), int(num2[1][0: -1])\n",
    "        \n",
    "        r_ans = r1 * r2 - v1 * v2 \n",
    "        v_ans = r1 * v2 + r2 * v1 \n",
    "\n",
    "        return '{}+{}i'.format(r_ans, v_ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        real1, image1 = map(int , num1[:-1].split('+'))\n",
    "        real2, image2 = map(int , num2[:-1].split('+'))\n",
    "\n",
    "        return f'{real1 * real2 - image1 * image2}+{real1 * image2 + image1 * real2}i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        num1=num1.split('+')\n",
    "        num2=num2.split('+')\n",
    "        num1[0]=int(num1[0])\n",
    "        num2[0]=int(num2[0])\n",
    "        num1[1]=int(num1[1][:-1])\n",
    "        num2[1]=int(num2[1][:-1])\n",
    "        res1=num1[1]*num2[1]*(-1)+num1[0]*num2[0]\n",
    "        res2=num1[0]*num2[1]+num2[0]*num1[1]\n",
    "        return str(res1)+'+'+str(res2)+'i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        r1,i1=map(int,num1[:-1].split('+'))\n",
    "        r2,i2=map(int,num2[:-1].split('+'))\n",
    "        return f\"{r1*r2-i1*i2}+{r1*i2+r2*i1}i\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        num1_list = num1.split(\"+\")\n",
    "        num1_real = int(num1_list[0])\n",
    "        num1_imaginary = int(num1_list[1][:-1])\n",
    "\n",
    "        num2_list = num2.split(\"+\")\n",
    "        num2_real = int(num2_list[0])\n",
    "        num2_imaginary = int(num2_list[1][:-1])\n",
    "\n",
    "        res_real = num1_real * num2_real - num1_imaginary *num2_imaginary\n",
    "        res_imaginary = num1_real * num2_imaginary + num2_real * num1_imaginary\n",
    "\n",
    "        return(\"{}+{}i\".format(res_real,res_imaginary))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        lst1=num1.split(\"+\")\n",
    "        lst2=num2.split(\"+\")\n",
    "        a=int(lst1[0])\n",
    "        b=int((lst1[1])[:-1])\n",
    "        c=int(lst2[0])\n",
    "        d=int((lst2[1])[:-1])\n",
    "        e=a*c-b*d\n",
    "        f=a*d+b*c\n",
    "        return str(e)+\"+\"+str(f)+\"i\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        data1 = num1.split('+')\n",
    "        data2 = num2.split('+')\n",
    "        \n",
    "        real1 = int( data1[0])\n",
    "        unreal1 = int(data1[1][:-1])\n",
    "       \n",
    "        \n",
    "        real2 = int(data2[0])\n",
    "        unreal2 = int (data2[1][:-1])\n",
    "        \n",
    "        # print(real1,unreal1)\n",
    "        \n",
    "        \n",
    "        # print(real2,unreal2)\n",
    "        \n",
    "        res_real = real1*real2 - (unreal1*unreal2)\n",
    "        res_unreal = real1*unreal2 + real2*unreal1 \n",
    "        \n",
    "     \n",
    "        res =  str(res_real) + '+' + str(res_unreal)+'i'\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\r\n",
    "#     def complexNumberMultiply(self, num1: str, num2: str) -> str:\r\n",
    "#         # 定义一个空白数组，记录两个数的 实部和虚部\r\n",
    "#         m = []\r\n",
    "#         for a, b in zip(num1, num2):\r\n",
    "#             x1, Y1 = num1.split(\"+\")\r\n",
    "#             x2, Y2 = num2.split(\"+\")\r\n",
    "#             y1, y2 = Y1.rstrip(\"i\"), Y2.rstrip(\"i\")\r\n",
    "#             m.extend([int(x1), int(y1), int(x2), int(y2)])\r\n",
    "#         return str(m[0] * m[2] - m[1] * m[3]) + \"+\" + str(m[0] * m[3] + m[1] * m[2]) + \"i\"\r\n",
    "\r\n",
    "\r\n",
    "class Solution:\r\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\r\n",
    "        # (a0 + b0*i) * (a1 + b1*i) = (a0 * a1 - b0 * b1) + (a0 * b1 + a1 * b0) * i\r\n",
    "        a0, b0 = map(int, num1[:-1].split(\"+\"))\r\n",
    "        a1, b1 = map(int, num2[:-1].split(\"+\"))\r\n",
    "        return \"{}+{}i\".format(a0 * a1 - b0 * b1, a0 * b1 + a1 * b0)\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\r\n",
    "class Solution:\r\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\r\n",
    "        n1 = re.match(r'(-?\\d+)\\+(-?\\d+)',num1)\r\n",
    "        n2 = re.match(r'(-?\\d+)\\+(-?\\d+)',num2)\r\n",
    "        a1 = int(n1.group(1))\r\n",
    "        b1 = int(n1.group(2))\r\n",
    "        a2 = int(n2.group(1))\r\n",
    "        b2 = int(n2.group(2))\r\n",
    "        a3 = a1 * a2 - b1 * b2\r\n",
    "        b3 = a1 * b2 + a2 * b1\r\n",
    "        return str(a3) + '+' + str(b3) + 'i'\r\n",
    "        \r\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        real1, imag1 = map(int, num1[:-1].split('+'))\n",
    "        real2, imag2 = map(int, num2[:-1].split('+'))\n",
    "        return f'{real1 * real2 - imag1 * imag2}+{real1 * imag2 + imag1 * real2}i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        real1, imag1 = map(int, num1[:-1].split('+'))\n",
    "        real2, imag2 = map(int, num2[:-1].split('+'))\n",
    "        return f'{real1 * real2 - imag1 * imag2}+{real1 * imag2 + imag1 * real2}i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        num1 = num1.split('+')\n",
    "        num2 = num2.split('+')\n",
    "        n1 = int(num1[0]) * int(num2[0]) - int(num1[1][:-1]) * int(num2[1][:-1])\n",
    "        n2 = int(num2[0]) * int(num1[1][:-1]) + int(num1[0]) * int(num2[1][:-1])\n",
    "\n",
    "        return str(n1) + '+' + str(n2) + 'i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        \"\"\"\n",
    "        (real1 * real2 - imag1 * imag2) + \n",
    "        (real1 * imag2 + imag1 * real2)i\n",
    "        \"\"\"\n",
    "        l1 = list(num1.split(\"+\"))\n",
    "        l2 = list(num2.split(\"+\"))\n",
    "        real1 = int(l1[0])\n",
    "        imag1 = int(l1[1][:-1])\n",
    "        real2 = int(l2[0])\n",
    "        imag2 = int(l2[1][:-1])\n",
    "        x = str(real1 * real2 - imag1 * imag2) + \"+\" + str(real1 * imag2 + imag1 * real2) + \"i\"\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        num1=num1.split('+')\n",
    "        num2=num2.split('+')\n",
    "        a=int(num1[0])*int(num2[0])-int(num1[1][:-1])*int(num2[1][:-1])\n",
    "        b=int(num1[0])*int(num2[1][:-1])+int(num1[1][:-1])*int(num2[0])\n",
    "        return str(a)+'+'+str(b)+'i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "\n",
    "        n1 = 0\n",
    "        cti_1 = 0\n",
    "        def f(s):\n",
    "            n = len(s)\n",
    "            idx = s.index('+')\n",
    "            return int(s[:idx]),int(s[idx+1:n-1])\n",
    "        n1,ctn_1 = f(num1)\n",
    "        n2,cnn_2 = f(num2)\n",
    "        s = n1*n2 + ctn_1*cnn_2*(-1)\n",
    "        p = n1*cnn_2+n2*ctn_1\n",
    "        print(n1,ctn_1, n2,cnn_2,s,p)\n",
    "        return str(s)+\"+\"+ str(p)+'i'\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        a1,b1 = map(int,num1[:-1].split('+'))\n",
    "        a2,b2 = map(int,num2[:-1].split('+'))\n",
    "        return f'{a1*a2-b1*b2}+{a1*b2+a2*b1}i'\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        items_1, items_2 = num1.split('+'), num2.split('+')\n",
    "        r1, v1 = int(items_1[0]), int(items_1[1][:-1])\n",
    "        r2, v2 = int(items_2[0]), int(items_2[1][:-1])\n",
    "        r = r1 * r2 - (v1 * v2)\n",
    "        v = v1 * r2 + v2 * r1\n",
    "        # print(r1, v1, r2, v2)\n",
    "        ans = str(r) + '+' + str(v) + 'i'\n",
    "        print(ans)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        x1, y1 = num1.split(\"+\")\n",
    "        x2, y2 = num2.split(\"+\")\n",
    "        y1 = int(y1[:-1])\n",
    "        y2 = int(y2[:-1])\n",
    "        x1 = int(x1)\n",
    "        x2 = int(x2)\n",
    "        x = x1 * x2 - y1 * y2\n",
    "        y = x1 * y2 + x2 * y1\n",
    "        return f\"{x}+{y}i\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        def parse(cnum: str):\n",
    "            return list(map(int, cnum[:-1].split('+')))\n",
    "\n",
    "        val1 = parse(num1)\n",
    "        val2 = parse(num2)\n",
    "\n",
    "        def prod(cval1: List[int], cval2: List[int]):\n",
    "            ret = [0, 0]\n",
    "            ret[0] = cval1[0]*cval2[0]-cval1[1]*cval2[1]\n",
    "            ret[1] = cval1[0]*cval2[1]+cval1[1]*cval2[0]\n",
    "            return ret\n",
    "        \n",
    "        rval = prod(val1, val2)\n",
    "\n",
    "        return '%d+%di'%(rval[0],rval[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# (real1 * real2 - image1 * image2) + (real1 * image2 + image1 * real2) * i\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        real1, image1 = num1.split('+')\n",
    "        real2, image2 = num2.split('+')\n",
    "        real1 = int(real1)\n",
    "        real2 = int(real2)\n",
    "        image1 = int(image1[:-1])\n",
    "        image2 = int(image2[:-1])\n",
    "        real = real1 * real2 - image1 * image2\n",
    "        image = real1 * image2 + image1 * real2\n",
    "        return str(real) + '+' + str(image) + 'i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        index_1 = num1.index('+')\n",
    "        real_1 = num1[:index_1]\n",
    "        i_1 = num1[index_1+1:-1]\n",
    "        \n",
    "        index_2 = num2.index('+')\n",
    "        real_2 = num2[:index_2]\n",
    "        i_2 = num2[index_2+1:-1]\n",
    "        \n",
    "        real = int(real_1)*int(real_2) - int(i_1) * int(i_2)\n",
    "        i = int(i_1)*int(real_2) + int(i_2)*int(real_1)\n",
    "        \n",
    "        result = str(real) + '+' + str(i) + 'i'\n",
    "        \n",
    "        return result      \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        real1, img1 = map(int, num1[:-1].split(\"+\"))\n",
    "        real2, img2 = map(int, num2[:-1].split(\"+\"))\n",
    "        return f'{real1 * real2 - img1 * img2}+{real1 * img2 + real2 * img1}i'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        a1, b1 = map(int, num1[0:-1].split('+'))\n",
    "        a2, b2 = map(int, num2[0:-1].split('+'))\n",
    "        return str(a1 * a2 - b1 * b2) + '+' + str(a1 * b2 + b1 * a2) + 'i'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        num1 = list(map(lambda x: int(x.replace(\"i\", \"\")), num1.split(\"+\")))\n",
    "        num2 = list(map(lambda x: int(x.replace(\"i\", \"\")), num2.split(\"+\")))\n",
    "        return f\"{num1[0] * num2[0] - num1[1] * num2[1]}+{num1[0] * num2[1] + num1[1] * num2[0]}i\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        real1 = int(num1[:num1.find(\"+\")])\n",
    "        comp1 = int(num1[num1.find(\"+\")+1:-1])\n",
    "        real2 = int(num2[:num2.find(\"+\")])\n",
    "        comp2 = int(num2[num2.find(\"+\")+1:-1])\n",
    "        res1 = real1*real2-comp1*comp2\n",
    "        res2 = real1*comp2+real2*comp1\n",
    "        return(str(res1)+\"+\"+str(res2)+\"i\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "\n",
    "        # 字符串处理\n",
    "\n",
    "        \n",
    "        # \n",
    "        # 思路：双栈，数字栈和符号栈\n",
    "\n",
    "    \n",
    "        # 分离实部和虚部\n",
    "        num1_list = num1.split(\"+\")\n",
    "        num2_list = num2.split(\"+\")\n",
    "        # 虚部有i，实部没有i\n",
    "    \n",
    "        a = int(num1_list[0])*int(num2_list[0])\n",
    "        b = int(num1_list[0])*int(num2_list[1][:-1])\n",
    "        c = int(num1_list[1][:-1])*int(num2_list[0])\n",
    "        d = int(num1_list[1][:-1])*int(num2_list[1][:-1])\n",
    "        ans = str(a-d)+\"+\"+str(b+c)+\"i\"\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        rc1 = num1.strip(\"i\").split(\"+\")\n",
    "        r1 = int(rc1[0])\n",
    "        c1 = int(rc1[1])\n",
    "        rc2 = num2.strip(\"i\").split(\"+\")\n",
    "        r2 = int(rc2[0])\n",
    "        c2 = int(rc2[1])\n",
    "        new_r = r1 * r2 - c1 * c2\n",
    "        new_c = r1 * c2 + r2 * c1\n",
    "        new_num = str(new_r) + \"+\" + str(new_c) + \"i\"\n",
    "        return new_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        r1, f1 = num1.split('+')[0], num1.split('+')[1]\n",
    "        r2, f2 = num2.split('+')[0], num2.split('+')[1]\n",
    "        fr1 = f1.replace('i','')\n",
    "        fr2 = f2.replace('i','')\n",
    "        r1,r2,fr1,fr2 = int(r1),int(r2),int(fr1),int(fr2)\n",
    "        res = r1 * r2 - fr1 * fr2\n",
    "        temp = str(r1 * fr2 + r2 * fr1) + 'i'\n",
    "        return str(res) + '+' + temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        a = num1.strip(\"i\").split(\"+\")\n",
    "        b = num2.strip(\"i\").split(\"+\")\n",
    "        ar = int(a[0]); ai = int(a[1])\n",
    "        br = int(b[0]); bi = int(b[1])\n",
    "        return str(ar*br-ai*bi) + \"+\" + str(ar*bi+br*ai) + \"i\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def complexNumberMultiply(self, num1: str, num2: str) -> str:\n",
    "        n1 = num1.split('+')\n",
    "        n2 = num2.split('+')\n",
    "        # print(n1)\n",
    "        n1 = [eval(n.replace('i', '')) for n in n1]\n",
    "        n2 = [eval(n.replace('i', '')) for n in n2]\n",
    "        res_real = n1[0]*n2[0] - n1[1]*n2[1]\n",
    "        res_v = n1[0]*n2[1] + n1[1]*n2[0]\n",
    "        return f'{res_real}+{res_v}i'"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
